package cn.jdy.plugins.wap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.socket.TextMessage;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.plugins.entity.Chat;
import cn.jdy.plugins.entity.ChatContent;
import cn.jdy.plugins.entity.ChatData;
import cn.jdy.plugins.entity.ExamQuestions;
import cn.jdy.plugins.entity.ChatRobot;
import cn.jdy.plugins.entity.Live;
import cn.jdy.plugins.entity.WatchUser;
import cn.jdy.plugins.service.ChatContentService;
import cn.jdy.plugins.service.ChatDataService;
import cn.jdy.plugins.service.ChatRobotService;
import cn.jdy.plugins.service.ChatService;
import cn.jdy.plugins.service.LiveService;
import cn.jdy.plugins.service.WatchUserService;
import cn.jdy.plugins.websocket.WebsocketEndPoint;

/**
 * @title  留言机器人定时设置
 *  @author jga
 * @date 2017-1-26
 */
@Controller
@RequestMapping("/chatRobot")
public class ChatRobotCtrl extends BaseCtrl {
	@Autowired
	private ChatRobotService chatRobotService;
	@Autowired
	private LiveService liveService;
	@Autowired
	private ChatContentService chatContentService;
	@Autowired
	private WatchUserService watchUserService;
	@Autowired
	private ChatDataService chatDataService;
	@Autowired
	private ChatService chatService;
	
	
	/**
	 * 生成机器留言数据
	 * @param ChatRobot
	 * @return
	 */
	@RequestMapping("/addRobot")
	@ResponseBody
	public Map<String, Object>  addRobot(ChatRobot obj) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();{
		try {
			 
			  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
	          String dsdata=df.format(new Date());// new Date()为获取当前系统时间
	       List<ChatData> rlist= chatDataService.findChatRobot(obj.getLiveId(),dsdata);
	       for (ChatData redData : rlist) {
	    	 
				 
				 	Chat chat = new Chat();
			    	chat.setLiveId(redData.getLiveId());
			    	chat.setUserOpenId(redData.getUserOpenId());
			    	chat.setUserNickName(redData.getUserNickName());
			    	chat.setUserHeadImg(redData.getUserHeadImg());
			    	chat.setUserSex(redData.getUserSex());
			    	chat.setChatType( redData.getChatType());
			    	chat.setChatContent( redData.getChatContent());
			    	chat.setStatus("0");
			    	String createTime = DateUtil.getCurrentDate(DateUtil._DEFAULT2);
			    	chat.setCreateTime(createTime);
			    	chatService.save(chat);
			    	
			    	//屏蔽脏字
			    	String reg = "[艹|日|靠|槽|逼|草|婊|淫|骚]";
					String reegmsg =  redData.getChatContent().replaceAll(reg, "*");
			    String json = "{'liveId':"+redData.getLiveId()+",'userOpenId':'"+redData.getUserOpenId()+"','userNickName':'"+redData.getUserNickName()+"','userHeadImg':'"+redData.getUserHeadImg()+"',"
			    			+ "'chatType':'"+redData.getChatType()+"','message':'"+ redData.getChatContent()+"','createTime':'"+redData.getCreateTime()+"'}";

		
			    WebsocketEndPoint wp=new WebsocketEndPoint();
			  //  wp.saveChat(redData.getLiveId()+"",user.getUserOpenId(),user.getUserNickName(),user.getUserHeadImg(),user.getUserSex(),chatType,msg);
				TextMessage textMessage = new TextMessage(json);
				wp.sendMessageToUsers(textMessage);//发送信息到前台
				System.out.println(json);
			    redData.setIsok("1");
		      	chatDataService.update(redData);//修改已经发送的
		}
	       
		
		} catch(Exception e) {
			BaseLog.e(getClass(), "保存异常！", e);
		}
		return jsonMap;
	}
 
}
	
	
	
	
	
	
}
